package com.hermes.biz.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import com.hermes.biz.common.model.comment.UserCommentDisplayUnitDO;
import com.hermes.biz.common.model.item.ItemUserRelationDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.manager.item.UserItemRelationManager;
import com.hermes.biz.service.user.UserConcernInfoService;
import com.hermes.biz.service.user.UserService;

public class UserConcernInfoServiceImpl implements UserConcernInfoService {

    private UserItemRelationManager userItemRelationManager;

    private UserService             userService;

    public List<UserCommentDisplayUnitDO> getUserConcernInfoList(long userId) {

        List<ItemUserRelationDO> userConcernItems = this.userItemRelationManager
            .getUserInterestedItems(userId);

        if (userConcernItems == null) {
            return new ArrayList<UserCommentDisplayUnitDO>();
        }

        List<UserCommentDO> concernCommentList = new ArrayList<UserCommentDO>();

        for (ItemUserRelationDO model : userConcernItems) {
            long itemId = model.getInterestedItemId();
            if (itemId > 0) {
                List<UserCommentDO> itemCommentList = this.userItemRelationManager
                    .getUserConcernItemCommentByItemId(itemId);
                if (itemCommentList != null) {
                    concernCommentList.addAll(itemCommentList);
                }
            }
        }

        if (concernCommentList == null || concernCommentList.size() < 1) {
            return new ArrayList<UserCommentDisplayUnitDO>();
        }

        List<UserCommentDisplayUnitDO> result = new ArrayList<UserCommentDisplayUnitDO>();

        for (UserCommentDO userCommentDO : concernCommentList) {
            UserCommentDisplayUnitDO unitDO = new UserCommentDisplayUnitDO();
            unitDO.setComment(userCommentDO.getComment());
            unitDO.setCreateTime(userCommentDO.getCreateTime());
            UserResultDO userDO = userService.getUserInfoByUserId(userCommentDO.getCreateUserId());
            unitDO.setHeadPic(userDO.getModule().getHeadPic());
            unitDO.setItemId(userCommentDO.getItemId());
            unitDO.setUserId(userCommentDO.getCreateUserId());
            unitDO.setUserName(userDO.getModule().getUserName());

            result.add(unitDO);

        }
        return result;
    }

    public UserItemRelationManager getUserItemRelationManager() {
        return userItemRelationManager;
    }

    public void setUserItemRelationManager(UserItemRelationManager userItemRelationManager) {
        this.userItemRelationManager = userItemRelationManager;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

}
